package com.pinc.cpbl;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class show_rank extends Activity {
	
	private ListView lv;
	private Button b_back;
	private Button b_reload;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waiting);
		new ShowRankTask().execute(getApplicationContext());
    }
	
	private class ShowRankTask extends AsyncTask<Context, Void, Integer> {
		private RankItem_Adapter rank_items;
		
		protected Integer doInBackground(Context... ctx) {
			rank_items = new RankItem_Adapter(ctx[0]);
			try {
				myHTMLparser mp = MyHTTPHelper.ConnectURL("http://www.cpbl.com.tw", ctx[0]);
				if ( mp == null)
				{
					return 1;
				}
				mp.stream.skip(8000);
				mp.gotoTagByProp("span", "id", "lblR1");
				mp.nextTag();
				mp.parseRankTable(rank_items);
			}
			catch(Exception e)
			{
				return 2;
			}
			return 0;
		}

		protected void onPostExecute(Integer err) {
			setContentView(R.layout.show_listview);
	        lv = (ListView)findViewById(R.id.listView1);
	        b_back = (Button) findViewById(R.id.b_back);
			b_reload = (Button) findViewById(R.id.b_act);
			TextView tv_title = (TextView) findViewById(R.id.textView1);
			
			tv_title.setText(R.string.title_rank);
			b_back.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});

			b_reload.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					setContentView(R.layout.waiting);
					new ShowRankTask().execute(show_rank.this);
				}
			});
			
	        if ( err == 1)
	        	Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_LONG).show();
	        else if (err == 2)
	        	Toast.makeText(getApplicationContext(), "Unknow Error", Toast.LENGTH_LONG).show();
	        lv.setAdapter(rank_items);
		}
	}

}
